\section{Código fuente}

En esta sección mostraremos el código que se encarga de generar el parser (en código java) para la gramática
presentada en las secciones anteriores.

Código del HTML.g:

\begin{verbatim}
grammar HTML;

options {
  language = Java;
  output = AST;
}

// Token lexicos
WS            : (' ' | '\t' | '\r'| '\n') {$channel=HIDDEN;};
TAG_html      : '<html>';
TAGF_html     : '</html>';
TAG_head      : '<head>';
TAGF_head     : '</head>';
TAG_body      : '<body>';
TAGF_body     : '</body>';
TAG_title     : '<title>';
TAGF_title    : '</title>';
TAG_script    : '<script>';
TAGF_script   : '</script>';
TAG_br        : '<br>';
TAG_div       : '<div>';
TAGF_div      : '</div>';
TAG_h1        : '<h1>';
TAGF_h1       : '</h1>';
TAG_p         : '<p>';
TAGF_p        : '</p>';
//En el texto no permitimos '<','>' ni '-' para evitar inconvenientes
Comentario: '<!-' .* '->'  {$channel=HIDDEN;};
Texto     : (' '|'0'..'9'|'a'..'z'|'A'..'Z'|'!'|'”'|'"'|'#'|'$'|'%'|'&'
|'/'|'('|')'|'='|'?'|'¡'|'¿'|'['|']'|'{'|'}'|'+'|'_'|'^')*;

                                                                        
// Gramatica

doc returns [String value]          
    : TAG_html pa=page TAGF_html
    {$value = "<html> <head> " +
    "<style TYPE='text/css'> " + 
    "DIV.bloque { margin-left: 2em;} " +
    "SPAN.TAG_head { color: orange;} " +
    "SPAN.TAGF_head { color: orange;} " + 
    "SPAN.TAG_title { color: fuchsia;} " +
    "SPAN.TAGF_title { color: fuchsia;} " +
    "SPAN.TAG_script { color: navy;} " +
    "SPAN.TAGF_script { color: navy;} " +
    "SPAN.TAG_body { color: lime;} " +
    "SPAN.TAGF_body { color: lime;} " +
    "SPAN.TAG_br { color: yellow;} " +
    "SPAN.TAG_div { color: brown;} " +
    "SPAN.TAGF_div { color: brown;} " +
    "SPAN.TAG_h1 { color : silver;} " +
    "SPAN.TAGF_h1 { color: silver;} " +
    "SPAN.TAG_p { color: red;} " +
    "SPAN.TAGF_p { color: red;} " +
    "</style> " +
    "</head> <body> " + 
     "<SPAN class='TAG_html'>&lt;html&gt;</SPAN>" +
     $pa.value +
     "<SPAN class='TAGF_html'>&lt;/html&gt;</SPAN>";};
page returns [String value]                               
    : h=header b=body {$value = $h.value + $b.value;};
header returns [String value]       
    : TAG_head lh=listH TAGF_head 
    {$value = "<DIV class='bloque'><SPAN class='TAG_head'>&lt;head&gt;</SPAN>"
    + $lh.value + "<SPAN class='TAGF_head'>&lt;/head&gt;</SPAN> </DIV>";}  
    | {$value ="";};
listH returns [String value]        
    : ti=titleOScript lh1=listH {$value = $ti.value + $lh1.value;} 
    | {$value ="";};
titleOScript returns [String value]
    : tit=title {$value = $tit.value;}
    | src=script {$value = $src.value;};
title returns [String value]        
    : TAG_title tex=Texto  TAGF_title
    {$value = "<DIV class='bloque'><SPAN class='TAG_title'>&lt;title&gt;</SPAN>"
    + $tex.text + "<SPAN class='TAGF_title'>&lt;/title&gt;</SPAN> </DIV>";} ;
script returns [String value]       
    : TAG_script texs=Texto TAGF_script
    {$value = "<DIV class='bloque'><SPAN class='TAGF_script'>&lt;script&gt;</SPAN>"
    + "<DIV class='bloque'>" + $texs.text + "</DIV>"
    + "<SPAN class='TAGF_script'>&lt;/script&gt;</SPAN> </DIV>";} ;
body returns [String value]         
    : TAG_body le=listaE TAGF_body
    {$value = "<DIV class='bloque'><SPAN class='TAG_body'>&lt;body&gt;</SPAN>"
    + "<DIV class='bloque'>" + $le.value + "</DIV>"
    + "<SPAN class='TAGF_body'>&lt;/body&gt;</SPAN> </DIV>";}
    | {$value ="";};
listaE returns [String value]       
    : tex1=Texto le1=listaE {$value = $tex1.text + $le1.value;} 
    | TAG_br le1=listaE {$value = "<SPAN class='TAG_br'>&lt;br&gt;</SPAN>" + $le1.value;} 
    | el=elem le1=listaE {$value = $el.value + $le1.value;}
    | {$value ="";};
elem returns [String value]
    : di=div {$value = $di.value;}
    | h11=h1 {$value = $h11.value;}
    | p1=p {$value = $p1.value;};
div returns [String value]          
    : TAG_div le2=listaE TAGF_div
    {$value = "<SPAN class='TAG_div'>&lt;div&gt;</SPAN>"
    + $le2.value + "<SPAN class='TAGF_div'>&lt;/div&gt;</SPAN>";} ;
h1 returns [String value]           
    : TAG_h1 le3=listaE TAGF_h1
    {$value = "<SPAN class='TAG_h1'>&lt;h1&gt;</SPAN>"
    + $le3.value + "<SPAN class='TAGF_h1'>&lt;/h1&gt;</SPAN>";} ;
p returns [String value]            
    : TAG_p le4=listaE TAGF_p 
    {$value = "<SPAN class='TAG_p'>&lt;p&gt;</SPAN>"
    + $le4.value + "<SPAN class='TAGF_p'>&lt;/p&gt;</SPAN>";} ;
\end{verbatim}